package com.java.comprehensive.other.wei;

import cn.hutool.core.thread.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * @Author 窦绍飞
 * @create 2023/2/15
 */
public class Test {

	public static void main(String[] args) {
		int a = 6;
		int b = 4;
		System.out.println(a & b);

		ThreadFactory threadFactory = new ThreadFactoryBuilder().setNamePrefix("arrayBlockingQueue").build();
		ThreadPoolExecutor.AbortPolicy abortPolicy = new ThreadPoolExecutor.AbortPolicy();
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 5, 100, TimeUnit.SECONDS, new LinkedBlockingDeque<>(500), threadFactory, abortPolicy);

		for (int i = 0; i < 10000; i++) {
			int finalI = i;
			threadPoolExecutor.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + ",执行" + finalI + "次");
				}
			});
		}
	}

	public void destroy(ThreadPoolExecutor poolExecutor) {
		try {
			poolExecutor.shutdown();
			if (!poolExecutor.awaitTermination(1000, TimeUnit.SECONDS)) {
				poolExecutor.shutdownNow();
			}
		} catch (InterruptedException e) {
			// 如果当前线程被中断，重新取消所有任务
			poolExecutor.shutdownNow();
			// 保持中断状态
			Thread.currentThread().interrupt();
		}
	}

}
